package smartbooks.ui.items;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import smartbooks.ui.manager.ItemManager;
import smartbooks.ui.util.CommonFunctions;
import smartbooksdb.hibernatebean.Item;

/**
 *
 * @author Ankit
 */
public class ItemsListPanel extends javax.swing.JPanel {
    ArrayList<Item> items = new ArrayList<Item>();
    ArrayList<Item> itemsInSearchTable = new ArrayList<Item>();
    String[] columnNames = {"No","Item Name","Item Code","Description","Selling Price","Tax","Qty"};
    Map<Long, String> vendorMap = new HashMap<Long, String>();
    Map<Long, String> taxMap = new HashMap<Long, String>();
    
    /**
     * Creates new form ItemsListPanel
     */
    public ItemsListPanel() {
        //setVendorMap();
        //setTaxMap();
        
        initComponents();
        
        customInitComponents();
    }
    
    private void customInitComponents() {
        addKeyListenersToComponents(keyListener);
        
        rebuildItemsListPanel();
    }
    
    private void rebuildItemsListPanel() {
        setAllItems();
        
        String itemNameSearch = (null==itemNameComboBox.getSelectedItem()) ? "" : itemNameComboBox.getSelectedItem().toString();
        String itemCodeSearch = (null==itemCodeComboBox.getSelectedItem()) ? "" : itemCodeComboBox.getSelectedItem().toString();
        String itemDescriptionSearch = (null==itemDescriptionComboBox.getSelectedItem()) ? "" : itemDescriptionComboBox.getSelectedItem().toString();
        String vendorNameSearch = (null==vendorNameComboBox.getSelectedItem()) ? "" : vendorNameComboBox.getSelectedItem().toString();
        String itemTaxSearch = (null==itemTaxComboBox.getSelectedItem()) ? "" : itemTaxComboBox.getSelectedItem().toString();

        populateSearchComboBoxes();

        itemNameComboBox.setSelectedItem(itemNameSearch);
        itemCodeComboBox.setSelectedItem(itemCodeSearch);
        itemDescriptionComboBox.setSelectedItem(itemDescriptionSearch);
        vendorNameComboBox.setSelectedItem(vendorNameSearch);
        itemTaxComboBox.setSelectedItem(itemTaxSearch);

        updateItemsListTable();
    }
    
    private void setAllItems() {
        this.items.clear();
        this.items = ItemManager.getAllActiveItems();
    }
    
    private void populateSearchComboBoxes() {
        itemNameComboBox.removeAllItems();
        itemCodeComboBox.removeAllItems();
        itemDescriptionComboBox.removeAllItems();
        vendorNameComboBox.removeAllItems();
        itemTaxComboBox.removeAllItems();
        
        for(Item item : items) {
            if(null!=item.getItemName() && !item.getItemName().equals("")) {
                itemNameComboBox.addItem(new MyComboBoxItem(item.getItemName()));
            }
            
            if(null!=item.getItemCode()&& !item.getItemCode().equals("")) {
                itemCodeComboBox.addItem(new MyComboBoxItem(item.getItemCode()));
            }
            
            if(null!=item.getItemDescription()&& !item.getItemDescription().equals("")) {
                itemDescriptionComboBox.addItem(new MyComboBoxItem(item.getItemDescription()));
            }
            
            if(item.getVendorId()>0) {
                
            }
            
            if(item.getItemTaxId()>0) {
                
            }
        }
        
        itemNameComboBox.setSelectedIndex(-1);
        itemCodeComboBox.setSelectedIndex(-1);
        itemDescriptionComboBox.setSelectedIndex(-1);
        vendorNameComboBox.setSelectedIndex(-1);
        itemTaxComboBox.setSelectedIndex(-1);
    }
    
    private void updateItemsListTable() {
        ArrayList<ArrayList<String>> itemsList = new ArrayList<ArrayList<String>>();
        
        String itemNameSearch = (null==itemNameComboBox.getSelectedItem()) ? "" : itemNameComboBox.getSelectedItem().toString().trim().toUpperCase();
        String itemCodeSearch = (null==itemCodeComboBox.getSelectedItem()) ? "" : itemCodeComboBox.getSelectedItem().toString().trim().toUpperCase();
        String itemDescriptionSearch = (null==itemDescriptionComboBox.getSelectedItem()) ? "" : itemDescriptionComboBox.getSelectedItem().toString().trim().toUpperCase();
        String vendorNameSearch = (null==vendorNameComboBox.getSelectedItem()) ? "" : vendorNameComboBox.getSelectedItem().toString().trim().toUpperCase();
        String itemTaxSearch = (null==itemTaxComboBox.getSelectedItem()) ? "" : itemTaxComboBox.getSelectedItem().toString().trim().toUpperCase();
        
        itemsInSearchTable.clear();
        int i = 1;
        for(Item item : items) {
            if(!itemNameSearch.equals("") && null!=item.getItemName() && item.getItemName().toUpperCase().contains(itemNameSearch)) {
                itemsList.add(putItemDataIntoList(item,i++));
                itemsInSearchTable.add(item);
            } else if(!itemCodeSearch.equals("") && null!=item.getItemCode() && item.getItemCode().toUpperCase().contains(itemCodeSearch)) {
                itemsList.add(putItemDataIntoList(item,i++));
                itemsInSearchTable.add(item);
            } else if(!itemDescriptionSearch.equals("") && null!=item.getItemDescription() && item.getItemDescription().toUpperCase().contains(itemDescriptionSearch)) {
                itemsList.add(putItemDataIntoList(item,i++));
                itemsInSearchTable.add(item);
            } else if(!vendorNameSearch.equals("")) {
                long vendorId = item.getVendorId();                
                String vendorName = vendorMap.get(vendorId);
                if(null!=vendorName && vendorName.toUpperCase().contains(vendorNameSearch)) {
                    itemsList.add(putItemDataIntoList(item,i++));
                    itemsInSearchTable.add(item);
                }
            } else if(!itemTaxSearch.equals("")) {
                long taxId = item.getItemTaxId();
                String taxName = taxMap.get(taxId);
                if(null!=taxName && taxName.toUpperCase().contains(itemTaxSearch)) {
                    itemsList.add(putItemDataIntoList(item,i++));
                    itemsInSearchTable.add(item);
                }
            } else if(itemNameSearch.equals("") && itemCodeSearch.equals("") && itemDescriptionSearch.equals("") && vendorNameSearch.equals("") && itemTaxSearch.equals("")) {
                itemsList.add(putItemDataIntoList(item,i++));
                itemsInSearchTable.add(item);
            }
        }
        
        ArrayList<String[]> tempList = new ArrayList<String[]>();
        for (ArrayList<String> stringList : itemsList){
            tempList.add((String[])stringList.toArray(new String[0]));
        }
        
        String[][] itemsListInTable = (String[][])tempList.toArray(new String[0][]);
        
        itemsListTable.setModel(new DefaultTableModel(itemsListInTable,columnNames) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        });
        
        itemsListTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {    
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(!e.getValueIsAdjusting()) {
                    deleteItemButton.setEnabled(itemsListTable.getSelectedRows().length>=1);
                    editItemButton.setEnabled(itemsListTable.getSelectedRows().length==1);
                }
            }
        }); 
        
        initColumnSizes();
    }
    
    private void initColumnSizes() {
        double[] columnSizes = {0.01,0.20,0.14,0.35,0.10,0.10,0.10};
        Dimension tableSize =  itemsListTable.getPreferredSize();
        
        for(int i=0; i<itemsListTable.getColumnCount(); i++) {
            itemsListTable.getColumnModel().getColumn(i).setPreferredWidth((int) Math.round(tableSize.width*columnSizes[i]));
        }
    }
    
    private ArrayList<String> putItemDataIntoList(Item item, int no) {
        ArrayList<String> itemList = new ArrayList<String>();
        
        itemList.add(String.valueOf(no));
        itemList.add(item.getItemName());
        itemList.add(item.getItemCode());
        itemList.add(item.getItemDescription());
        itemList.add(String.valueOf(item.getSellingPrice()));
        // Put tax properly
        //itemList.add(String.valueOf(item.getItemTaxId()));
        itemList.add("");
        if(item.isManageInventoryStocks()) {
            itemList.add(String.valueOf(item.getCurrentQuantity()));
        } else {
            itemList.add("");
        }
        
        return itemList;
    }
    
    private void addKeyListenersToComponents(KeyListener keyListener) {
        ((JTextField)itemNameComboBox.getEditor().getEditorComponent()).addKeyListener(keyListener);
        ((JTextField)itemCodeComboBox.getEditor().getEditorComponent()).addKeyListener(keyListener);
        ((JTextField)itemDescriptionComboBox.getEditor().getEditorComponent()).addKeyListener(keyListener);
        ((JTextField)vendorNameComboBox.getEditor().getEditorComponent()).addKeyListener(keyListener);
        ((JTextField)itemTaxComboBox.getEditor().getEditorComponent()).addKeyListener(keyListener);
    }
    
    private static class MyComboBoxItem {
        private String value;

        public MyComboBoxItem(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return value; //this is what display in the JComboBox
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        itemsListPanel = new javax.swing.JPanel();
        itemsListToolBar = new javax.swing.JToolBar();
        addItemButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        deleteItemButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        editItemButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        itemNameComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        itemCodeComboBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        itemDescriptionComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        vendorNameComboBox = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        itemTaxComboBox = new javax.swing.JComboBox();
        resetButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        itemsListTable = new javax.swing.JTable();

        itemsListPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Items List", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        itemsListToolBar.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        itemsListToolBar.setFloatable(false);
        itemsListToolBar.setRollover(true);

        addItemButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        addItemButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/add_24-24.png"))); // NOI18N
        addItemButton.setText("Add");
        addItemButton.setFocusable(false);
        addItemButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addItemButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemButtonActionPerformed(evt);
            }
        });
        itemsListToolBar.add(addItemButton);

        jLabel1.setText("   ");
        itemsListToolBar.add(jLabel1);

        deleteItemButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        deleteItemButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/delete_24-24.png"))); // NOI18N
        deleteItemButton.setText("Delete");
        deleteItemButton.setEnabled(false);
        deleteItemButton.setFocusable(false);
        deleteItemButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteItemButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        deleteItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteItemButtonActionPerformed(evt);
            }
        });
        itemsListToolBar.add(deleteItemButton);

        jLabel2.setText("   ");
        itemsListToolBar.add(jLabel2);

        editItemButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        editItemButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smartbooks/images/edit_24-24.png"))); // NOI18N
        editItemButton.setText("Edit");
        editItemButton.setEnabled(false);
        editItemButton.setFocusable(false);
        editItemButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editItemButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editItemButtonActionPerformed(evt);
            }
        });
        itemsListToolBar.add(editItemButton);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Search Criteria", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Item Name:");

        itemNameComboBox.setEditable(true);
        itemNameComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Item Code:");

        itemCodeComboBox.setEditable(true);
        itemCodeComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Item Description:");

        itemDescriptionComboBox.setEditable(true);
        itemDescriptionComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Vendor Name:");

        vendorNameComboBox.setEditable(true);
        vendorNameComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Item Tax:");

        itemTaxComboBox.setEditable(true);
        itemTaxComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        resetButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        resetButton.setText("Reset");
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });

        searchButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(itemNameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(itemCodeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(itemTaxComboBox, 0, 201, Short.MAX_VALUE)
                                    .addComponent(vendorNameComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addComponent(itemDescriptionComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(resetButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(searchButton)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {resetButton, searchButton});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(itemNameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(vendorNameComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(itemCodeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(itemTaxComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(itemDescriptionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(resetButton))
                .addContainerGap())
        );

        itemsListTable.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        itemsListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "No", "Item Name", "Item Code", "Item Description", "Selling Price", "Tax", "Qty"
            }
        ));
        itemsListTable.setFocusable(false);
        itemsListTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                itemsListTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(itemsListTable);

        javax.swing.GroupLayout itemsListPanelLayout = new javax.swing.GroupLayout(itemsListPanel);
        itemsListPanel.setLayout(itemsListPanelLayout);
        itemsListPanelLayout.setHorizontalGroup(
            itemsListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemsListPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(itemsListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 624, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(itemsListToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 624, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        itemsListPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {itemsListToolBar, jPanel1, jScrollPane1});

        itemsListPanelLayout.setVerticalGroup(
            itemsListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemsListPanelLayout.createSequentialGroup()
                .addComponent(itemsListToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(itemsListPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(itemsListPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        updateItemsListTable();
    }//GEN-LAST:event_searchButtonActionPerformed

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
        itemNameComboBox.setSelectedItem("");
        itemCodeComboBox.setSelectedItem("");
        itemDescriptionComboBox.setSelectedItem("");
        vendorNameComboBox.setSelectedItem("");
        itemTaxComboBox.setSelectedItem("");

        updateItemsListTable();
    }//GEN-LAST:event_resetButtonActionPerformed

    private void addItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemButtonActionPerformed
        ItemsDialog itemsDialog = new ItemsDialog(CommonFunctions.getParentJFrame(this), true, null);
        Item item = itemsDialog.showDialog();
        if(null!=item) {
            rebuildItemsListPanel();
        }
    }//GEN-LAST:event_addItemButtonActionPerformed

    private void deleteItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteItemButtonActionPerformed
        int deleteConfirmation = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete the selected item(s) ?", "SmartBooks - Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

        if(deleteConfirmation==0) {
            for(int row : itemsListTable.getSelectedRows()) {
                ItemManager.deleteItem(itemsInSearchTable.get(row));
            }
            
            rebuildItemsListPanel();
            
            JOptionPane.showMessageDialog(this,"Item(s) is/are deleted successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_deleteItemButtonActionPerformed

    private void editItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editItemButtonActionPerformed
        int selectedRow = itemsListTable.getSelectedRow();
        
        if(selectedRow>=0) {
            Item selectedItem = (itemsInSearchTable.get(selectedRow)).clone();
            ItemsDialog itemsDialog = new ItemsDialog(CommonFunctions.getParentJFrame(this), true, selectedItem);
            Item item = itemsDialog.showDialog();
            if(null!=item) {
                rebuildItemsListPanel();
            }
        } else {
            JOptionPane.showMessageDialog(this,"No row is selected to edit item !","SmartBooks - Warning",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_editItemButtonActionPerformed

    private void itemsListTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_itemsListTableMouseClicked
        if(evt.getClickCount()==2) {
            editItemButtonActionPerformed(null);
        }
    }//GEN-LAST:event_itemsListTableMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addItemButton;
    private javax.swing.JButton deleteItemButton;
    private javax.swing.JButton editItemButton;
    private javax.swing.JComboBox itemCodeComboBox;
    private javax.swing.JComboBox itemDescriptionComboBox;
    private javax.swing.JComboBox itemNameComboBox;
    private javax.swing.JComboBox itemTaxComboBox;
    private javax.swing.JPanel itemsListPanel;
    private javax.swing.JTable itemsListTable;
    private javax.swing.JToolBar itemsListToolBar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton searchButton;
    private javax.swing.JComboBox vendorNameComboBox;
    // End of variables declaration//GEN-END:variables
    
    KeyListener keyListener = new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) { }

        @Override
        public void keyPressed(KeyEvent e) { }

        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER) { 
                searchButtonActionPerformed(null);
            }
        }
    };
}